<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
//击鼓传花

function Queue (){

    this.items = [];

    //将元素加入到队列中
    Queue.prototype.enqueue = function(element){
        this.items.push(element);

    };

    //从队列中删除前端元素
    //数组用来删除前面的元素，效率不高（会将后面的元素全部向前移）
    Queue.prototype.dequeue = function (){
        return this.items.shift();

    };

    //查看前端的元素
    Queue.prototype.front = function(){
        return this.items[0];
    };

    //查看队列是否为空
    Queue.prototype.isEmpty = function(){
        return this.items.length == 0;

    };

    //查看队列中元素的个数
    Queue.prototype.size = function(){
        return this.items.length;
    };

    //toString方法
    Queue.prototype.toString = function(){
        var resultString = '';
        for(var i = 0;i < this.items.length;i++){
            resultString += this.items[i]+" ";

        }
        return resultString;

    };

}


function passGame(nameList,num){
    var queue = new Queue();
    for(var i = 0;i < nameList.length;++i){
        queue.enqueue(nameList[i]);
    }
    // console.log(queue.toString());
    //开始数数字

    //不是num的时候，重新加入到队列的末尾
    //是num这个数字的时候，将其从队列中删除
    //num 数字之前的人重新放到数字的末尾

    // alert(queue)
    while(queue.size()>1) {
        for (var i = 0; i < num - 1; i++) {
            queue.enqueue(queue.dequeue());
        }
        queue.dequeue();
        // console.log(queue.toString());
        // console.log(queue);
    }
    var endEle = queue.front();
    return endEle;


}

var result = passGame(["lilei","wangmei","xiaoqiang","liming","xiaowei","zhangsan","lisi"],4);
console.log(result);

//将所有人加入到队列中
</script>
</body>
</html>